package com.flyway.controller.captcha;


import com.alibaba.fastjson.JSON;
import com.flyway.anno.PassToken;
import com.flyway.pojo.Users;
import com.flyway.pojo.captcha.ImageVerificationDto;
import com.flyway.pojo.captcha.ImageVerificationVo;
import com.flyway.service.CaptchaService;
import com.flyway.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: 滑动验证码
 * -------------------
 * @Author: xupx
 */

@Controller
@RequestMapping("/nologin/captcha")
@Slf4j
public class CaptchaController{
    /**
     * 运算码业务处理对象
     */
    @Autowired
    private CaptchaService captchaService;
    @Autowired
    private UserService userService;

    /**
     * 调用内部freemarker模板
     * @return 跳转index.ftl
     */
    @RequestMapping("/index")
    public String index() {
        return "indexs";
    }

    /**
     * 获取验证码
     * @param imageVerificationDto 验证码参数
     * @return 根据类型参数返回验证码
     */
    @RequestMapping("/get/verification/image")
    @ResponseBody
    @PassToken
    public ImageVerificationVo getVerificationImage(ImageVerificationDto imageVerificationDto) {
        ImageVerificationVo imageVerificationVo = null;
        try {
            Users zhangsan = userService.findUserByUserName("zhangsan");
            log.info(JSON.toJSONString(zhangsan));
            imageVerificationVo = captchaService.selectSlideVerificationCode(imageVerificationDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
        return imageVerificationVo;
    }

    /**
     * 校验验证码
     * @param x x轴坐标
     * @param y y轴坐标
     * @return 验证结果
     */
    @ResponseBody
    @RequestMapping("/check/verification/result")
    public boolean checkVerificationResult(String token,String x, String y) {
        boolean result = false;
        try {
            result = captchaService.checkVerificationResult(token,x, y);
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
        return result;
    }
}